<?php

namespace App\Repositories;

use App\Enum\CarEnum;
use App\Models\Car;
use BobCoder\Construct\Foundation\Repository\Repository;

class CarRepository extends Repository
{
    protected $model;

    public function __construct()
    {
        $this->model = new Car();
    }

    /**
     * 分页列表
     * @param array $condition
     * @param string $number 车牌号
     * @param int $companyId 公司ID
     * @param int $pageSize
     * @return mixed
     * @Author 姿势就是力量
     */
    public function getPageList(array $condition, int $companyId,int $brand,int $restriction,string $number,string  $real_name,string $date,string $colour,string $owner,int $pageSize = 10)
    {
        return $this->model->select(['id','colour','owner', 'number', 'type', 'restriction', 'ownership', 'model', 'status', 'company_id', 'brand_id','created_at','updated_at'])
            ->with(['brand:id,name', 'company:id,name', 'driver:real_name,host', 'service:id,name'])
            ->where($condition)
            ->company($companyId)
            ->when($number, function ($query) use ($number) {
                $query->where('number', 'like', '%' . $number . '%'); //车牌号
            })
            ->when($colour, function ($query) use ($colour) {
                $query->where('colour', 'like', '%' . $colour . '%'); //颜色
            })
            ->when($owner, function ($query) use ($owner) {
                $query->where('owner', 'like', '%' .$owner. '%');  //所属公司/个人
            })
            ->when($brand,function ($query) use($brand){
                   $query->where('brand_id',$brand);            //车辆品牌
            })
            ->when($real_name,function ($query)use ($real_name){
                $query->whereHas('driver',function ($query2) use ($real_name){
                   $query2->where('real_name','like','%'.$real_name.'%');   //司机姓名
                });
            })
            ->when($restriction,function ($query)use ($restriction){
                $query->whereHas('driver',function ($query2) use ($restriction){
                    $query2->where('restriction','like','%'.$restriction.'%');  //限行
                });
            })
            ->when($date, function ($query) use ($date) {
                if($date){
                    $list = explode(',',$date);
                    $query->whereBetween('created_at', [$list[0],$list[1]]);  //时间选择
                }
            })
            ->orderBy('created_at', 'desc')
            ->paginate($pageSize)
            ->toArray();
    }


    /**
     * 车辆信息
     * @param int $carId 车辆ID
     * @return mixed
     * @Author 姿势就是力量
     */
    public function getFirst(int $carId)
    {
        //增加字段返回车辆品牌
        return $this->model->where('id', $carId)
            ->with(['driver:id,real_name,phone', 'front', 'side', 'back','brand:id,name'])
            ->first();
    }


    /**
     * 保存信息
     * @param $params
     * @return Car|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
     * @Author 姿势就是力量
     */
    public function saveData(array $params)
    {
        if (!empty($params['id'])) {
            $model = $this->find($params['id']);
        } else {
            $model = $this->model;
        }

        foreach ($params as $key => $value) {
            $model->{$key} = $value;
        }
        $model->save();

        return $model;
    }

    /**
     * 车辆列表
     * @param string $number
     * @param array $columns
     * @return mixed
     * @Author 姿势就是力量
     */
    public function getList(string $number, array $columns = ['*'])
    {
        return $this->model->select($columns)
            ->with('company:id,name','brand:id,name','driver:id,host,real_name','service:id,name')
            ->when($number, function ($query) use ($number) {
                $query->where('number', 'like', '%' . $number . '%');
            })->company()
            ->pagination()
            ->get()
            ->toArray();
    }


}
